<?php
/*************************************************************************************
 *
 *	FileName: EDPImage.php
 *	Desc    : Class to manipulate the picture
 *	Author  : Seal
 *	Date    : 2010-1-14
 *
 *************************************************************************************/
require_once ( LIB_DIR.'classes/MagickWand.php');

/**
 * change to invoke magickwand php extension
 */
class EDPImage {
	private $magickwand;
	private $quality = '100';

	public function __construct() {
		$this->magickwand = new Magick();
	}

	public function Resize( $source, $destination, $width, $height = 0, $exactDimentions = false ) {
	//		$modifier = $exactDimentions ? '!' : '>';
	//
	//		//if $width or $height == 0 then we want to resize to fit one measure
	//		//if any of them is sent as 0 resize will fail because we are trying to resize to 0 px
	//		$width = $width == 0 ? '' : $width;
	//		$height = $height == 0 ? '' : $height;
	//
	//		$cmd = '';
	//		$cmd .= ' -scale "' . $width . 'x' . $height . $modifier;
	//		$cmd .= '" -quality ' . $this->quality;
	//		$cmd .= ' -strip ';
	//		$cmd .= ' "' . $source . '" "' . $destination . '"';
	//
	//		exec( 'convert ' . $cmd );

		//$width = $width == 0 ? '' : $width;
		//$height = $height == 0 ? '' : $height;
		$this->magickwand->LoadFromFile( $source );
		$this->magickwand->ResizeTo( $width, $height );
		$this->magickwand->Save($destination, $this->quality);
	}

	public function CropAvatar( $source, $x1, $y1, $w, $h )
	{
		$source = preg_replace( "/s\.jpg$/i", ".jpg", $source );
		list($width, $height, $type, $attr) = getimagesize( $source );
	
		if( $w > $width )
			$w = $width;
		
		if( $h > $height )
			$h = $height;

		$this->magickwand->LoadFromFile( $source );
		$this->magickwand->Crop($x1, $y1, $w, $h);
		/**
		 * add by bo
		 * Resize after crop
		 */
		$this->magickwand->ResizeTo( SMALL_AVATAR_WIDTH, SMALL_AVATAR_WIDTH );				
		$source = preg_replace( "/\.jpg$/i", "s.jpg", $source );
		$this->magickwand->Save($source, $this->quality);
		
	}
	public function SaveAvatar( $source, $needResize=true ) {
		list($width, $height, $type, $attr) = getimagesize( $source );

		// Firstly transform the format to jpg
		$destination = preg_replace( "/\..+$/i", ".jpg", $source );
	
		$this->ToFormat( $source, $destination );

		//crop image to square
		$left = 0;
		$top = 0;
		if( $width > $height && $width > SMALL_AVATAR_WIDTH ){
			$left = ($width-$height)/2;
			$width = $height;
		}
		else if( $height > $width && $height > SMALL_AVATAR_WIDTH ){
			$top = ($height-$width)/2;
			$height = $width;
		}
		else{
			//do nothing
		}
		$this->magickwand->Crop($left, $top, $width, $height);
		$this->magickwand->ResizeTo( SMALL_AVATAR_WIDTH, SMALL_AVATAR_WIDTH );
		$destination_s = preg_replace( "/\..+$/i", "s.jpg", $source );
		$this->magickwand->Save($destination_s, $this->quality);
		
		if( $source != $destination )
			@unlink( $source );

		return $destination_s;
	}

	public function SavePicture( $source, $needResize = true ) {
		list($width, $height, $type, $attr) = getimagesize( $source );
		
		// Firstly transform the format to jpg
		$destination = preg_replace( "/\..+$/i", ".jpg", $source );
		$this->ToFormat( $source, $destination );
		$this->PutWaterLogo( $destination );
		
		if( $needResize ) {
		// Secondely make a copy and scale it to a smaller one
			$destination = preg_replace( "/\..+$/i", "s.jpg", $source );

			if( $width > SMALL_PIC_MAX_WIDTH && $height > SMALL_PIC_MAX_HEIGHT )
			{
				if( $width >= $height )
				{
					$height = $height * (SMALL_PIC_MAX_WIDTH / $width); 
					$width = SMALL_PIC_MAX_WIDTH;
				}
				else
				{
					$width = $width * (SMALL_PIC_MAX_HEIGHT / $height); 
					$height = SMALL_PIC_MAX_HEIGHT;
				}
				$this->Resize( $source, $destination, $width, $height );
			}
			else if( $width > SMALL_PIC_MAX_WIDTH )
			{
				$height = $height * (SMALL_PIC_MAX_WIDTH / $width); 	
				$this->Resize( $source, $destination, SMALL_PIC_MAX_WIDTH, $height );
			}
			else if( $height > SMALL_PIC_MAX_HEIGHT )
			{
				$width = $width * (SMALL_PIC_MAX_HEIGHT / $height);
				$this->Resize( $source, $destination, $width, SMALL_PIC_MAX_HEIGHT );
			}
			else
				$this->ToFormat( $source, $destination );
			
			$this->PutWaterLogo( $destination );
		}
	}
	
	 public function PutWaterLogo( $fromFilePath )
	 {  
		system( "composite -gravity southeast ". WARTER_LOGO_PATH . " $fromFilePath $fromFilePath" );
		return;
 
	 	 $fromWand = NewMagickWand();
	 	 $fromFile = MagickReadImage($fromWand, $fromFilePath);
	 	 
	 	 
	 	 $waterWand = NewMagickWand();
	 	 $waterFile = MagickReadImage($waterWand,  WARTER_LOGO_PATH );
	 	 
        
	 	 $fromFileWidth = MagickGetImageWidth( $fromWand);
	 	 $fromFileHeight = MagickGetImageHeight( $fromWand );
	 	 
	 	 $waterFileWidth = MagickGetImageWidth( $waterWand);
	 	 $waterFileHeight = MagickGetImageHeight( $waterWand );
	 	 
	 	 if( $fromFileHeight >= $waterFileHeight && $fromFileWidth >= $waterFileWidth ) {
	 	 	$topY = $fromFileHeight - $waterFileHeight;
	 	 	$topX = $fromFileWidth - $waterFileWidth;
	 	 	$re = MagickCompositeImage( $fromWand, $waterWand, MW_AtopCompositeOp, $topX, $topY );
	 	 	MagickSetFormat ( $fromWand, 'jpg');
            		$result = MagickWriteImage( $fromWand, $fromFilePath );
			$logger = GetLogger();
			$logger->error( "water mark result: $re : $result" );
	 	 }
     }   
	
	public function ToFormat( $source, $destination ) {
//		$cmd = '';
//		$cmd .= ' -quality ' . $this->quality;
//		$cmd .= ' "' . $source . '" "' . $destination . '"';
//		exec( 'convert ' . $cmd );
		$this->magickwand->LoadFromFile( $source );
		$this->magickwand->Save($destination, $this->quality);
	}
	
	public function IsValidPicture( $file )
	{
		$Wand = NewMagickWand();

    	if( !MagickPingImage($Wand, $file) )
    		return false;
    		
    	return true;
	}
}
?>
